import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { apiMenu } from '@/api/user.js'

// 异步请求的Thunk action creator
export const setMenu = createAsyncThunk(
  'menu/setMenu',
  async () => {
    const res = await apiMenu();
    return res;
  }
);

export const counterSlice = createSlice({
  name: "menu",
  initialState: {
    menu: []
  },
  extraReducers: (builder) => {
    builder
      .addCase(setMenu.fulfilled, (state, {payload}) => {
        state.menu  = payload.data;
      })
      .addCase(setMenu.rejected, (state) => {
        state.menu  = [];
      });
  },
});

export default counterSlice.reducer;
